import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RecordIncomeDialog extends JDialog {
    private JTextField dateField;
    private JTextField amountField;
    private JTextField categoryField;
    private JTextField remarksField;

    public RecordIncomeDialog(JFrame owner) {
        super(owner, "记录收入", true);
        setSize(400, 250);
        setLocationRelativeTo(owner); // 居中显示
        setLayout(new GridLayout(5, 2, 10, 10));

        // 创建输入字段
        dateField = new JTextField();
        amountField = new JTextField();
        categoryField = new JTextField();
        remarksField = new JTextField();

        // 添加标签和输入字段
        add(new JLabel("日期 (yyyy-MM-dd):"));
        add(dateField);
        add(new JLabel("金额:"));
        add(amountField);
        add(new JLabel("类别:"));
        add(categoryField);
        add(new JLabel("备注:"));
        add(remarksField);

        // 创建保存按钮
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String dateStr = dateField.getText().trim();
                String amountStr = amountField.getText().trim();
                String category = categoryField.getText().trim();
                String remarks = remarksField.getText().trim();

                // 收集所有错误信息
                StringBuilder errorMessages = new StringBuilder();

                if (!validateDate(dateStr)) {
                    errorMessages.append("请输入有效的日期格式 (yyyy-MM-dd)。\n");
                }

                if (!validateAmount(amountStr)) {
                    errorMessages.append("金额必须为正数。\n");
                }

                // 如果有错误信息，则显示并返回
                if (errorMessages.length() > 0) {
                    JOptionPane.showMessageDialog(RecordIncomeDialog.this, errorMessages.toString(), "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 保存记录
                saveIncomeRecord(dateStr, amountStr, category, remarks);
                JOptionPane.showMessageDialog(RecordIncomeDialog.this, "收入记录已保存。");
                dispose(); // 关闭对话框
            }
        });

        // 添加保存按钮
        add(saveButton);
    }

    private boolean validateDate(String dateStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        try {
            Date date = dateFormat.parse(dateStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private boolean validateAmount(String amountStr) {
        try {
            double amount = Double.parseDouble(amountStr);
            return amount > 0;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    private void saveIncomeRecord(String date, String amount, String category, String remarks) {
        String filePath = "Gitzuoye/src/income";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            writer.write(date + "," + amount + "," + category + "," + remarks);
            writer.newLine();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "无法保存记录，请检查文件路径或权限。", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}